package com.jumi.microservice.common.utils;

import com.jumi.microservice.common.base.dto.AbstractEnum;

public class EnumUtil {
    /**
     * 根据code获取枚举指定类型
     *
     * @param code
     * @param enumClass
     * @param <T>
     * @return
     */
    public static <T extends AbstractEnum> T getByCode(Integer code, Class<T> enumClass) {
        for (T each : enumClass.getEnumConstants()) {
            if (code.equals(each.getCode())) {
                return each;
            }
        }
        return null;
    }

    public static <T extends AbstractEnum> T getByCode(Byte code, Class<T> enumClass) {
        for (T each : enumClass.getEnumConstants()) {
            if (code.equals(each.getCode())) {
                return each;
            }
        }
        return null;
    }

    public static <T extends AbstractEnum> T getByCode(String code, Class<T> enumClass) {
        for (T each : enumClass.getEnumConstants()) {
            if (code.equals(each.getCode())) {
                return each;
            }
        }
        return null;
    }
}
